home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / MicImagePlugin.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  1.9 KB  |  95 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: MicImagePlugin.py,v 1.1.1.1 1998/08/18 13:07:50 sjoerd Exp $
  4. #
  5. # Microsoft Image Composer support for PIL
  6. #
  7. # Notes:
  8. #    uses TiffImagePlugin.py to read the actual image streams
  9. #
  10. # History:
  11. #    97-01-20 fl    Created
  12. #
  13. # Copyright (c) Secret Labs AB 1997.
  14. # Copyright (c) Fredrik Lundh 1997.
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18.  
  19.  
  20. __version__ = "0.1"
  21.  
  22. import string
  23.  
  24. import Image, TiffImagePlugin
  25. from OleFileIO import *
  26.  
  27.  
  28. #
  29. # --------------------------------------------------------------------
  30.  
  31.  
  32. def _accept(prefix):
  33.     return prefix[:8] == MAGIC
  34.  
  35.  
  36. class MicImageFile(TiffImagePlugin.TiffImageFile):
  37.  
  38.     format = "MIC"
  39.     format_description = "Microsoft Image Composer"
  40.  
  41.     def _open(self):
  42.  
  43.     # read the OLE directory and see if this is a likely
  44.     # to be a Microsoft Image Composer file
  45.  
  46.     try:
  47.         self.ole = OleFileIO(self.fp)
  48.     except IOError:
  49.         raise SyntaxError, "not an MIC file; invalid OLE file"
  50.  
  51.     # find ACI subfiles with Image members (maybe not the
  52.     # best way to identify MIC files, but what the... ;-)
  53.  
  54.     self.images = []
  55.     for file in self.ole.listdir():
  56.         if file[1:] and file[0][-4:] == ".ACI" and file[1] == "Image":
  57.         self.images.append(file)
  58.  
  59.     # if we didn't find any images, this is probably not
  60.     # an MIC file.
  61.     if not self.images:
  62.         raise SyntaxError, "not an MIC file; no image entries"
  63.  
  64.     self.__fp = self.fp
  65.     self.frame = 0
  66.  
  67.     if len(self.images) > 1:
  68.         self.category = Image.CONTAINER
  69.  
  70.     self.seek(0)
  71.  
  72.     def seek(self, frame):
  73.  
  74.     try:
  75.         filename = self.images[frame]
  76.     except IndexError:
  77.         raise EOFError, "no such frame"
  78.  
  79.     self.fp = self.ole.openstream(filename)
  80.  
  81.     TiffImagePlugin.TiffImageFile._open(self)
  82.  
  83.     self.frame = frame
  84.  
  85.     def tell(self):
  86.  
  87.     return self.frame
  88.  
  89. #
  90. # --------------------------------------------------------------------
  91.  
  92. Image.register_open("MIC", MicImageFile, _accept)
  93.  
  94. Image.register_extension("MIC", ".mic")
  95.